<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>Bootloader Development</title>
		<link rel="stylesheet" type="text/css" href="./images/style.css">
	</head>
	
	<body>
		<header class="banner">
			<h1>PIC16系列MCU 串口自举程序（Bootloader）开发</h1>
		</header>
		
		<main>
			<section>
				<h2>1. 简介</h2>
				<article>
					<p>本示例为基于MCC环境快速开发的PIC16系列MCU(PIC16F1769)的串口自举程序。</p>
				</article>
			</section>
			
			<section>
				<h2>2. 工具</h2>
				<article>
					<header>
					<h3>2.1. 软件开发环境</h3>
					</header>
					<p>相关软件开发版本情况说明如下。</p>
					<ul>
					<li><a href="https://www.microchip.com/mplabx" target="_blank">Microchip MPLAB® xIDE version 5.50 or newer</a></li>
					<li><a href="https://www.microchip.com/en-us/development-tools-tools-and-software/mplab-xc-compilers" target="_blank">Microchip MPLAB® XC8 C compiler version 2.32 or newer</a></li>
					<li><a href="https://www.microchip.com/en-us/development-tools-tools-and-software/embedded-software-center/mplab-code-configurator" target="_blank">Microchip MPLAB® Code Configurator (MCC) version 4.2.1 (with core v5.2.3) or newer</a></li>
					<li><a href="https://www.microchip.com/en-us/development-tools-tools-and-software/embedded-software-center/mplab-code-configurator" target="_blank">Microchip MPLAB® Code Configurator (MCC) Device Libraries PIC10 / PIC12 / PIC16 / PIC18 MCUs version 1.81.7 or newer</a></li>
					<li><a href="https://www.microchip.com/en-us/development-tools-tools-and-software/embedded-software-center/mplab-code-configurator" target="_blank">Microchip 18-bit Bootloader version 2.4.1 or newer</a></li>
					<li><a href="https://packs.download.microchip.com/" target="_blank">Microchip PIC12-16F1xxx Series Device Support version 1.2.63 or newer</a></li>
					</ul>
				</article>
				<article>
					<header>
					<h3>2.2. 硬件开发环境</h3>
					</header>
					<p>该例程基于如下硬件开发。</p>
					<ul>
					<li><a href="https://www.microchip.com/en-us/development-tool/DM164137" target="_blank">CURIOSITY LOW PIN DEVELOPMENT BOARD (Part Number: DM164137)</a></li>
					<img src="./images/3576-201018-mcu8-dm164137-low-pin-count-curiosity-board-md.jpg" width="10%"; style="margin-left: 40px;">
					<li><a href="https://www.microchip.com/en-us/product/PIC16F1769#buy-from-store" target="_blank">PIC16F1769-I/P</a></li>
					<img src="./images/medium-PIC16F1769-PDIP-20.png" width="7%"; style="margin-left: 40px;">
					<li><a href="https://www.microchip.com/en-us/development-tool/ADM00393" target="_blank">MCP2200 BREAKOUT MODULE (Part Number: ADM00393)</a></li>
					<img src="./images/814-adm00393.jpg" width="7%"; style="margin-left: 40px;">
					</ul>
				</article>
				<article>
					<header>
					<h3>2.3. 参考资料</h3>
					</header>
					<p>相关参考资料如下。</p>
					<ul>
					<li><a href="https://www.microchip.com/en-us/product/PIC16F1769" target="_blank">PIC16F1769 Data Sheet</a></li>
					<li><a href="https://www.microchip.com/en-us/development-tool/DM164137" target="_blank">Curiosity Low Pin Count Development Board User's Guide</a></li>
					<li><a href="https://www.microchip.com/en-us/development-tool/DM164137" target="_blank">Curiosity LPC Schematics Rev 2</a></li>
					<li><a href="https://www.microchip.com/en-us/development-tool/ADM00393" target="_blank">MCP2200 Breakout Module User's Guide</a></li>
					<li><a href="https://ww1.microchip.com/downloads/en/DeviceDoc/40001779B.pdf" target="_blank">Bootloader Generator User's Guide</a></li>
					</ul>
				</article>
			</section>
			
			<section>
				<h2>3. 自举程序工程创建</h2>
				<article>
					<header>
						<h3>3.1. 打开MPLAB® xIDE，选择"File->New Project"。</h3>
					</header>
					<img src="./images/boot_setup_1.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.2. 选择"Standalone Project"，点击下一步。</h3>
					</header>
					<img src="./images/boot_setup_2.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.3. 输入器件型号，点击下一步。</h3>
					</header>
					<img src="./images/boot_setup_3.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.4. 选择编译器版本，点击下一步。</h3>
					</header>
					<img src="./images/boot_setup_4.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.5. 输入工程项目名称，并选择相应存取路径后点击完成按钮，完成空工程的创建。</h3>
					</header>
					<img src="./images/boot_setup_5.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.6. 选择"Tools->Embedded->MPLAB Code Configurator v4: Open/Close"选项，打开MCC开发环境。</h3>
					</header>
					<img src="./images/boot_setup_6.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.7. 在System Module下按如下设置系统时钟。</h3>
					</header>
					<img src="./images/boot_setup_7.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.8. 在器件资源列表里添加8位bootloader generator库到项目资源列表中，并按如下设置。</h3>
					</header>
					<img src="./images/boot_setup_8.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.9. 添加外设资源MEMORY和EUSART到项目资源列表中，这里检查一下EUSART的配置情况，采用该默认配置即可。</h3>
					</header>
					<img src="./images/boot_setup_9.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.10. 按下图对相关管脚进行设置，其中RB7是串口接收引脚、RC7是串口发送引脚；RC4是好奇板的S1按键，RA5是好奇板的D4。</h3>
					</header>
					<img src="./images/boot_setup_10.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.11. 点击"Generate"按钮，生成代码，弹出的确认框点击"Yes"。</h3>
					</header>
					<img src="./images/boot_setup_11.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.12. 设置自举程序工程代码空间范围。</h3>
					</header>
					<img src="./images/boot_setup_12.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.13. 点击下图编译按钮，自举程序工程可成功编译。到此，自举程序工程开发完毕。</h3>
					</header>
					<img src="./images/boot_setup_13.png" width="80%">
				</article>
			</section>
			
			<section>
				<h2>4. 应用程序工程创建</h2>
				<article>
					<header>
						<h3>4.1. 选择"File->New Project"。</h3>
					</header>
					<img src="./images/app_setup_1.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.2. 选择"Standalone Project"，点击下一步。</h3>
					</header>
					<img src="./images/app_setup_2.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.3. 输入器件型号，点击下一步。</h3>
					</header>
					<img src="./images/app_setup_3.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.4. 选择编译器版本，点击下一步。</h3>
					</header>
					<img src="./images/app_setup_4.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.5. 输入工程项目名称，并选择相应存取路径后点击完成按钮，完成空工程的创建。</h3>
					</header>
					<img src="./images/app_setup_5.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.6. 选择"Tools->Embedded->MPLAB Code Configurator v4: Open/Close"选项，打开MCC开发环境。</h3>
					</header>
					<img src="./images/app_setup_6.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.7. 在System Module下按如下设置系统时钟。</h3>
					</header>
					<img src="./images/app_setup_7.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.8. 添加外设资源TMR0到项目资源列表中，并按如下设置。该TMR0用于实现好奇板D7的500ms闪烁。</h3>
					</header>
					<img src="./images/app_setup_8.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.9. 按下图对相关管脚进行设置，其中RA2是好奇板的D6，RC5是好奇板的D7。</h3>
					</header>
					<img src="./images/app_setup_9.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.10. 点击"Generate"按钮，生成代码，</h3>
					</header>
					<img src="./images/app_setup_10.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.11. 对main.c文件做如下更改，其中右侧为更改后文件内容。使能中断，TMR0_UserInterruptHandler()函数用于D7 500ms闪烁，在while(1)循环中实现D6的500ms闪烁。</h3>
					</header>
					<img src="./images/app_setup_11.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.12. 设置应用程序工程代码的地址偏移，此偏移根据自举程序工程代码空间范围而定。</h3>
					</header>
					<img src="./images/app_setup_12.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.13. 应用程序工程编译后执行hexmate语句"hexmate ${ImagePath} -FILL=0x3FFF@0x0000:0x3FFB -CK=0x0800-0x3FFB@0x3FFC+0x0w-2t34g2 -o${ImagePath}"，该语句用于生成应用程序代码的和校验，校验规则与自举程序工程相匹配。</h3>
					</header>
					<img src="./images/app_setup_13.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.14. 点击下图编译按钮，应用程序工程可成功编译。到此，应用程序工程开发完毕。</h3>
					</header>
					<img src="./images/app_setup_14.png" width="80%">
				</article>
			</section>
			
			<section>
				<h2>5. 串口升级实验验证</h2>
				<article>
					<header>
						<h3>5.1. 桌面实验平台搭建如下。</h3>
					</header>
					<img src="./images/desk_demo.jpg" width="40%">
				</article>
				<article>
					<header>
						<h3>5.2. 用usb线缆连接好奇开发板的J2口，烧录串口自举程序。烧录程序后可以看到好奇开发板的D4常亮。</h3>
					</header>
					<img src="./images/verify_1.png" width="80%">
					<img src="./images/verify_2.png" width="80%">
				</article>
				<article>
					<header>
						<h3>5.3. 用另一根usb线缆链接MCP2200，并明确其串口号，接着对scripts文件夹下的serial_upload.bat进行相应修改，之后点击该批处理文件完成升级工作。升级成功后，D6和D7以500ms闪烁。</h3>
					</header>
					<img src="./images/verify_3.png" width="80%">
				</article>
				<article>
					<header>
						<h3>5.4. 若再次升级，需要同时按住好奇开发板的MCLR按键和S1按键，然后先释放MCLR按键，再释放S1按键，既可以再次重复5.3步骤。</h3>
					</header>
				</article>
				<article>
					<header>
						<h3>5.5. 在项目归档或生产中，有时需要将自举和应用程序工程hex合并，合并可通过运行scripts文件夹下的hex_merge_app_boot.bat实现，具体参数配置需要与工程相匹配。</h3>
					</header>
					<img src="./images/verify_4.png" width="80%">
				</article>
				<article>
					<header>
						<h3>5.6. 运行hex_merge_app_boot.bat后，打开MPLAB® xIPE。烧录../app_pic16f1769.X/dist/default/production/app_pic16f1769.X.merged.hex。</h3>
					</header>
					<img src="./images/verify_5.png" width="80%">
					<img src="./images/verify_6.png" width="80%">
				</article>
				<article>
					<header>
						<h3>5.7. 若需要升级，则可同时按住好奇开发板的MCLR按键和S1按键，然后先释放MCLR按键，再释放S1按键，接着可以重复5.3步骤完成升级工作。</h3>
					</header>
				</article>
			</section>
		</main>
	</body>
</html>